iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 9
0
Modern Web

用30days 了解web系列 第 9

(Day 09) retrieving database data using API method

  • 分享至 

  • xImage
  •  

today we will cover some api and axios

Create Your own api
write your api url under your routes/api.php. I'm writting like this:
https://ithelp.ithome.com.tw/upload/images/20200922/201296097pVLw1xjcx.png

https://ithelp.ithome.com.tw/upload/images/20200922/20129609ES531E7W1E.png

the getapi function was actually using eloquent method to get the data from database called student_data table and turn the data structure into the json model.
after you create your own api you can use you own third party software to make a request to your api and get the data, for example in this article I'll use AXIOS. axios will send a request using nodejs, so we will take the response in our script. after we get what we wanted in javascript we could append it to our html pages.
for those who don't know axios go check this link.

AXIOS
import axios using cdn method:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

make the request

axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

after you get the resposne you could append the response value into your HTML page using either javascript or jquery.


上一篇
(Day 08) what is RESTful API
下一篇
(Day 10) Laravel Auth and JWT
系列文
用30days 了解web30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言